//
//  ABWelcomeController.swift
//  microblog
//
//  Created by 安波 on 15/10/30.
//  Copyright © 2015年 anbo. All rights reserved.
//

import UIKit
import SDWebImage

class ABWelcomeController: UIViewController {
    
    // MARK: - 属性
    /// 头像底部约束
    private var iconViewBottomCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 准备UI
        prepareUI()
        
        if let urlString = ABUserAccount.loadAccount()?.avatar_large{
            // 设置用户的头像
            iconView.ab_setImageWithURL(NSURL(string: urlString), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //开始动画
        iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        
        // usingSpringWithDamping: 值越小弹簧效果越明显 0 - 1
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1.0, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            // 重新布局UI
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    // 设置透明度
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        // 切换控制器
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(true)
                })
        }
        
    }
    
    
    
    // MARK: - 准备UI
    private func prepareUI(){
        // 添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 添加约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        /*
            H | 父控件的左边 | 父控件的右边
            V | 父控件的顶部 | 父控件的底部
         */
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg": backgroundImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : backgroundImageView]))
        
        // 头像
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        // 重置底部160  别忘记添加到View上  iconViewBottomCons是可选类型，需要强拆
        iconViewBottomCons = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottomCons!)
        
        // 欢迎归来
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
    }
    

    // MARK: 懒加载
    /// 背景
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    /// 头像
    private lazy var iconView: UIImageView = {
        // 获得头像图片
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        // 修剪边框，切成圆形
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    /// 欢迎归来
    private lazy var welcomeLabel: UILabel = {
        // 创建一个label
        let label = UILabel()
        // 设置文字
        label.text = "欢迎归来"
        // 设置透明度
        label.alpha = 0
        
        return label
    }()

}
